/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Panel;

import DAO.KhachHangDao;
import DAO.NhanVienDAO;
import Gui.MainForm;
import javax.swing.JOptionPane;
import util.Global;

/**
 *
 * @author Steven
 */
public class DangNhap extends javax.swing.JPanel {

    NhanVienDAO nvDao = null;
    KhachHangDao khDao = null;
    MainForm parent = null;
    
    private String user = "";
    private String pass = "";
    private String port = "";
    private String database = "";
    private String host = "";
    
    /**
     * Creates new form DangNhap
     */
    public DangNhap(String User ,String Pass,String port,String DataBase,String Host,MainForm parent) {
        initComponents();
        this.user = User;
        this.pass = Pass;
        this.port = port;
        this.database = DataBase;
        this.host = Host;
        
        this.parent = parent;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        groupEmployeeCustomer = new javax.swing.ButtonGroup();
        groupDatabase = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        JTFUsername = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        JRBEmployee = new javax.swing.JRadioButton();
        JRBCustomer = new javax.swing.JRadioButton();
        JRBDatabaseError = new javax.swing.JRadioButton();
        JRBDatabaseFixed = new javax.swing.JRadioButton();
        JBLogin = new javax.swing.JButton();
        JBCancle = new javax.swing.JButton();
        JTFPassword = new javax.swing.JPasswordField();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Đăng nhập"));

        jLabel1.setText("Tên Đăng Nhập:");

        jLabel2.setText("Mật Khẩu:");

        groupEmployeeCustomer.add(JRBEmployee);
        JRBEmployee.setSelected(true);
        JRBEmployee.setText("Nhân Viên");

        groupEmployeeCustomer.add(JRBCustomer);
        JRBCustomer.setText("Khách Hàng");

        groupDatabase.add(JRBDatabaseError);
        JRBDatabaseError.setText("CSDL Lỗi");

        groupDatabase.add(JRBDatabaseFixed);
        JRBDatabaseFixed.setSelected(true);
        JRBDatabaseFixed.setText("CSDL Đã Sửa Lỗi");

        JBLogin.setText("Đăng Nhập");
        JBLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBLoginActionPerformed(evt);
            }
        });

        JBCancle.setText("Hủy");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(JTFUsername)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(JBLogin)
                                .addGap(18, 18, 18)
                                .addComponent(JBCancle, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(JRBEmployee)
                                    .addComponent(JRBDatabaseError))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(JRBDatabaseFixed)
                                    .addComponent(JRBCustomer)))
                            .addComponent(JTFPassword))
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(JTFUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(JTFPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JRBEmployee)
                    .addComponent(JRBCustomer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JRBDatabaseError)
                    .addComponent(JRBDatabaseFixed))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JBLogin)
                    .addComponent(JBCancle))
                .addContainerGap(15, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void JBLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBLoginActionPerformed
        // TODO add your handling code here:
        if(JRBDatabaseError.isSelected()){
            Global.databaseError = true;
            this.parent.database = util.Constants.DATABASE_ERROR;
            this.database = util.Constants.DATABASE_ERROR;
        }else{
            Global.databaseError = false;
            this.parent.database = util.Constants.DATABASE_FIXED;
            this.database = util.Constants.DATABASE_FIXED;
        }
        
        
        
        nvDao = new NhanVienDAO(user, pass, port, this.database, host);
        khDao = new KhachHangDao(user, pass, port, this.database, host);
        
        String err = "";
        if(JRBCustomer.isSelected()){
            err= khDao.DangNhap(JTFUsername.getText(), JTFPassword.getText());
        }else{
            err = nvDao.DangNhap(JTFUsername.getText(), JTFPassword.getText());
        }
        
        if(!err.equals("")){
            JOptionPane.showMessageDialog(this, err,"Thông báo",JOptionPane.ERROR_MESSAGE);
        }else{
            util.Global.isLogin = true;
            this.parent.enableFuntions();
        }
    }//GEN-LAST:event_JBLoginActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JBCancle;
    private javax.swing.JButton JBLogin;
    private javax.swing.JRadioButton JRBCustomer;
    private javax.swing.JRadioButton JRBDatabaseError;
    private javax.swing.JRadioButton JRBDatabaseFixed;
    private javax.swing.JRadioButton JRBEmployee;
    private javax.swing.JPasswordField JTFPassword;
    private javax.swing.JTextField JTFUsername;
    private javax.swing.ButtonGroup groupDatabase;
    private javax.swing.ButtonGroup groupEmployeeCustomer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables
}
